package org.ouwh.fw.model.data;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.ListIterator;

import org.ouwh.fw.util.xml.Node;

public class List extends Data implements java.util.List<Struct> {
	/**
	 * 
	 */
	private static final long serialVersionUID = -1300134458688264496L;
	ArrayList<Struct> elements = new ArrayList<Struct>(128);

	public List() {
		super();
		this.tagName = "list";
	}

	public Object init(Node node) {
		super.init(node);
		return this;
	}

	public void addData(Data data) {
		add((Struct) data);
	}

	public boolean add(Struct struct) {
		return elements.add(struct);
	}

	public Struct get(int idx) {
		return elements.get(idx);
	}

	public Struct remove(int idx) {
		return elements.remove(idx);
	}

	public void clear() {
		elements.clear();
	}

	public int size() {
		return elements.size();
	}

	public String toString() {
		StringBuffer tmp = new StringBuffer(1024);
		for (int i = 0; i < tabCount; i++) {
			tmp.append('\t');
		}
		tmp.append("<");
		tmp.append(tagName);
		tmp.append(" id=\"");
		tmp.append(getName());
		tmp.append("\" >\n");
		tabCount++;
		for (int i = 0; i < this.elements.size(); i++) {
			Struct node = (Struct) elements.get(i);
			tmp.append(node.toString());
		}
		tabCount--;
		for (int i = 0; i < tabCount; i++) {
			tmp.append("\t");
		}
		tmp.append("</");
		tmp.append(tagName);
		tmp.append(">\n");
		return tmp.toString();
	}

	public Object clone() throws CloneNotSupportedException {
		try {
			List cloned = new List();
			cloned.setName(getName());
			cloned.tagName = this.tagName;
			for (int i = 0; i < size(); i++) {
				cloned.add((Struct) get(i).clone());
			}
			return cloned;
		} catch (Exception e) {
			throw new CloneNotSupportedException(e.getMessage());
		}
	}

	@Override
	public void add(int index, Struct element) {
		elements.add(index, element);
	}

	@Override
	public boolean addAll(Collection<? extends Struct> c) {
		return elements.addAll(c);
	}

	@Override
	public boolean addAll(int index, Collection<? extends Struct> c) {
		return elements.addAll(index, c);
	}

	@Override
	public boolean contains(Object o) {
		return elements.contains(o);
	}

	@Override
	public boolean containsAll(Collection<?> c) {
		return elements.containsAll(c);
	}

	@Override
	public int indexOf(Object o) {
		return elements.indexOf(o);
	}

	@Override
	public boolean isEmpty() {
		return elements.isEmpty();
	}

	@Override
	public Iterator<Struct> iterator() {
		return elements.iterator();
	}

	@Override
	public int lastIndexOf(Object o) {
		return elements.lastIndexOf(o);
	}

	@Override
	public ListIterator<Struct> listIterator() {
		return elements.listIterator();
	}

	@Override
	public ListIterator<Struct> listIterator(int index) {
		return elements.listIterator(index);
	}

	@Override
	public boolean remove(Object o) {
		return elements.remove(o);
	}

	@Override
	public boolean removeAll(Collection<?> c) {
		return elements.removeAll(c);
	}

	@Override
	public boolean retainAll(Collection<?> c) {
		return elements.retainAll(c);
	}

	@Override
	public Struct set(int index, Struct element) {
		return elements.set(index, element);
	}

	@Override
	public java.util.List<Struct> subList(int fromIndex, int toIndex) {
		return elements.subList(fromIndex, toIndex);
	}

	@Override
	public Object[] toArray() {
		return elements.toArray();
	}

	@Override
	public <T> T[] toArray(T[] a) {
		return elements.toArray(a);
	}
}
